<?php
 /**
  *  @package ARXLib
  *  @version 2
  *  @subpackage Inputs
  *
  *  @copyright Ringo-Web Inc. 2004 - 2005
  */
//////////////////////////////////////////////////////////////////////////////////////
// CARXUploadFile
//////////////////////////////////////////////////////////////////////////////////////



    class CARXUploadFile extends CARXInput
    {
        var $m_fileFolder;
        var $m_width;
        var $m_keyFieldName;
        var $m_ThumbPath;
        var $m_ThumbWidth;
        var $m_ThumbHeight;
        var $m_thumbResizeOptions;

        var $m_WidthFieldName;
        var $m_HeightFieldName;
        var $m_resizeWidth;
        var $m_resizeHeight;
        var $m_resizeOptions;

        // Additional thumbnail sizes
        //      subdir
        //      width
        //      height
        var $m_addThumbs;

        function CARXUploadFile($label, $dbFieldName, $keyFieldName, $fileFolder, $width = 24, $addtag = "")
        {
            parent::CARXInput($label, $dbFieldName);
            $this->m_width = $width;
            $this->m_fileFolder = $fileFolder;
            $this->m_keyFieldName = $keyFieldName;
            $this->m_ThumbnailPath = "";
            $this->m_ThumbWidth = 0;
            $this->m_ThumbHeight = 0;
            $this->m_resizeWidth = 0;
            $this->m_resizeHeight = 0;

            $this->m_WidthFieldName = "";
            $this->m_HeightFieldName = "";
            $this->m_TagParams = $addtag;
        }

        function SetSizeFieldNames($widthField, $heightField)
        {
            $this->m_WidthFieldName = $widthField;
            $this->m_HeightFieldName = $heightField;
        }

        function EnableThumbnails($thumbPath, $width, $height, $resize_options = 0)
        {
            $this->m_ThumbPath = $thumbPath;
            $this->m_ThumbWidth = $width;
            $this->m_ThumbHight = $height;
            $this->m_thumbResizeOptions = $resize_options;
        }

        // Enable resizing image upon uploading.
        // if $height == 0 or $height == 0 then image is resized proportionally
        function ResizeImages($width, $height, $resize_options = 0)
        {
            $this->m_resizeWidth = $width;
            $this->m_resizeHeight = $height;
            $this->m_resizeOptions = $resize_options;
        }

        // Funstion is used to setup additional thumbnails created
        // while uploading image
        function AddImageThumbnail($width, $height, $dir, $options = 0)
        {
            $thumbopt->width = $width;
            $thumbopt->height = $height;
            $thumbopt->dir = $dir;
            $thumbopt->options = $options;
            $this->m_addThumbs[] = $thumbopt;
        }

        function GetHTML()
        {
            if ($this->GetDBTable())
            {
	        	$getKeyTable = klon($this->GetDBTable());
	            $getKeyTable->Open();
	            $oldFileName = $getKeyTable->GetField($this->m_dbFieldName);
	            $getKeyTable->Close();
            }
            else
            {
            	$oldFileName = '';
            }

            //$result = parent::GetHTML();
            $htmlCtrlName = $this->GetHTMLCtrlName();
            if ($oldFileName) $result .= "<input type=\"checkbox\" name=\"$htmlCtrlName"."_delete\"> ".ArxMsg("MSG_ARX_DELETE")."<br>\n";
            $result .= "<input type=\"file\" name=\"$htmlCtrlName\" value=\"$this->m_value\" size=$this->m_width>\n";
            return $result;
        }

        function DeleteFile($fileName, $bThumbnail = FALSE)
        {
            if ($bThumbnail)
            {
                $fullName = $this->m_ThumbPath . "/$fileName";
            }
            else
            {
                $fullName = $this->m_fileFolder . "/$fileName";
            }
            if (is_file($fullName))
            {
                unlink($fullName);
            }
        }

        // Resize image from $srcFileName and save it to $destFileName
        // $srcFileName can be equal to $destFileName
        // if $newHeight == 0 or $newWidth == 0 then image resized proportionally
        function ResizeImage($srcFileName, $destFileName, $newWidth, $newHeight, $resize_options)
        {
            $size = GetImageSize($srcFileName);
            $width = $size[0];
            $height = $size[1];
            $type = $size[2];

            if (!$newWidth && !$newHeight)
            {
                $newWidth = $width;
                $newHeight = $height;
            }
            else
            {
                if (!$newHeight)
                {
                    $newHeight = intval($newWidth*$height/$width);
                }
                else if (!$newWidth)
                {
                    $newWidth = intval($newHeight*$width/$height);
                }
                else
                {
                    if ($resize_options & UPLOAD_RESIZE_PROPORTIOANAL_NOT_MORE)
                    {
                        $height1 = intval($newWidth*$height/$width);
                        if ($height1 > $newHeight)
                        {
                            $newWidth = intval($newHeight*$width/$height);
                        }
                        else
                        {
                            $newHeight = $height1;
                        }
                    }
                    else if ($resize_options & UPLOAD_RESIZE_PROPORTIOANAL_NOT_LESS)
                    {
                        $height1 = intval($newWidth*$height/$width);
                        if ($height1 < $newHeight)
                        {
                            $newWidth = intval($newHeight*$width/$height);
                        }
                        else
                        {
                            $newHeight = $height1;
                        }
                    }

                }
            }

            //$new_im = ImageCreate($newWidth, $newHeight);
            $new_im = ImageCreateTrueColor($newWidth, $newHeight);
            switch ($type)
            {
                case "1" :
                    $im = ImageCreateFromGIF($srcFileName);
                    ImageCopyResampled($new_im, $im, 0, 0, 0, 0,
                        $newWidth, $newHeight , $width, $height);

                    if ($srcFileName == $destFileName)
                    {
                        unlink($destFileName);
                    }
                    //ImageGIF($new_im, $destFileName);
                    ImageJPEG($new_im, $destFileName);
                    chmod($destFileName, 0777);
                    break;
                case "2" :
                    $im = ImageCreateFromJPEG($srcFileName);
                    $dw = 0;
                    $dh = 0;
                    if ($resize_options == UPLOAD_CUT_AND_RESIZE)
                    {
                        $kw = $width/$newWidth;
                        $kh = $height/$newHeight;
                        if ($kw > $kh) $dw = round(($width-$kh*$newWidth)/2)-1;
                        else $dh = round(($height-$kw*$newHeight)/2)-1;
                    }

                    ImageCopyResampled($new_im, $im, 0, 0, $dw, $dh,
                        $newWidth, $newHeight , $width-2*$dw, $height-2*$dh);

                    if ($srcFileName == $destFileName)
                    {
                        unlink($destFileName);
                    }

                    ImageJPEG($new_im, $destFileName, 90);
                    chmod($destFileName, 0777);
                    break;
                case "3" :
                    $im = ImageCreateFromPNG($srcFileName);
                    ImageCopyResampled($new_im, $im, 0, 0, 0, 0,
                        $newWidth, $newHeight , $width, $height);

                    if ($srcFileName == $destFileName)
                    {
                        unlink($destFileName);
                    }

                    ImagePNG($new_im, $destFileName);
                    chmod($destFileName, 0777);
                    break;
            }
        }

        // This function creates additional thumbnails axxording to
        // calls to AddImageThumbnail(...)
        function CreateAdditionalThumbnails($srcName)
        {
            if (is_array($this->m_addThumbs) and count($this->m_addThumbs))
            {
                $addThumbs = $this->m_addThumbs;
                for ($i = 0; $i < count($addThumbs); $i++)
                {
                    $this->CreateAdditionalThumbnail($srcName, $addThumbs[$i]);
                }
            }
        }

        function CreateAdditionalThumbnail($srcName, $addThumb)
        {
            $srcFullName = $this->m_fileFolder . "/$srcName";
            $destFullName = $addThumb->dir . "/$srcName";

            $this->ResizeImage($srcFullName, $destFullName,
                $addThumb->width, $addThumb->height, $addThumb->options);
        }

        function Submit()
        {
            //parent::Submit();

            // Getting record key and old file name
            if ($this->GetDBTable())
            {
	            $getKeyTable = klon($this->GetDBTable());
	            $getKeyTable->Open();
	            $oldFileName = $getKeyTable->GetField($this->m_dbFieldName);
	            $keyID = $getKeyTable->GetField($this->m_keyFieldName);
	            $getKeyTable->Close();
            }
            else
            {
            	$oldFileName = '';
            }

            $table =& $this->GetDBTable();

            $file = $_FILES[$this->GetHTMLCtrlName()];
           	if ($file["type"] == "application/octet-stream")
           	{
           		return false;
           	}
            if ($file)
            {
                if ($file['tmp_name'])
                {
                	$fileName = uniqid('') . '.' . substr(strrchr($file['name'], "."), 1);
                	if ($oldFileName)
                	{
                    	$this->DeleteFile($oldFileName);
                	}
                    if ($table) $table->SetField($this->m_dbFieldName, $fileName);

                    $fullName = $this->m_fileFolder . "/$fileName";
                    move_uploaded_file($file['tmp_name'], $fullName);
                    chmod($fullName, 0777);

                    // Set Size field values
                    $size = GetImageSize($fullName);
                    //$this->m_resizeWidth,
                    //$this->m_resizeHeight
                    if ($this->m_WidthFieldName)
                    {
                        if ($table) $table->SetField($this->m_WidthFieldName, $size[0]);
                    }

                    if ($this->m_HeightFieldName)
                    {
                        if ($table) $table->SetField($this->m_HeightFieldName, $size[1]);
                    }

                    if ($this->m_ThumbPath)
                    {
                        $this->DeleteFile($oldFileName, TRUE);
                        $thumbName = $this->m_ThumbPath . "/$fileName";
                        // Create Thumbnail
                        $this->ResizeImage($fullName, $thumbName,
                            $this->m_ThumbWidth, $this->m_ThumbHight, $this->m_thumbResizeOptions);

                    $this->CreateAdditionalThumbnails($fileName);

                    // There was a problem with accessing file
                    // via HTTP after move_uploaded_file
                    // So resize it anyway. In case if new size is 0x0 this
                    // will just overwrite current image

                    // Resize uploaded image if needed
                    $this->ResizeImage($fullName, $fullName,
                        $this->m_resizeWidth, $this->m_resizeHeight, $this->m_resizeOptions);
                    }
                }
            }
        }

    }
?>